package com.ruoyi.omchannel.helper;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.ruoyi.omchannel.domain.OmChannel;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Base64;

@Component
public class QRCodeGenerator {

    private static final int WHITE = 0xFFFFFF;
    private static final int BLACK = 0x000000;

    private static final String IMG_PATH = "D:\\ruoyi\\uploadPath\\upload\\jie"; // 设置默认存储路径

    /**
     * 根据给定的文字内容生成二维码的Base64字符串
     *
     * @param content 需要生成二维码的文字内容
     * @return 返回二维码的Base64编码字符串
     */
    public String generateQRCodeAsBase64(String content) {
        try {
            Map<EncodeHintType, String> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 500, 500, hints);
            BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB);

            for (int x = 0; x < bitMatrix.getWidth(); x++) {
                for (int y = 0; y < bitMatrix.getHeight(); y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
                }
            }

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ImageIO.write(image, "png", stream);
            return Base64.getEncoder().encodeToString(stream.toByteArray());

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String generateQRCodeToFile(String content) {
        try {
            Map<EncodeHintType, String> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 500, 500, hints);
            BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB);

            for (int x = 0; x < bitMatrix.getWidth(); x++) {
                for (int y = 0; y < bitMatrix.getHeight(); y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
                }
            }

            // 确保目录存在
            File directory = new File(IMG_PATH);
            if (!directory.exists()) {
                directory.mkdirs();
            }

            // 文件名可以使用UUID或其他唯一标识来避免冲突
            String imgName = content + ".png"; // 或者根据需要调整文件名
            File file = new File(directory, imgName);

            ImageIO.write(image, "png", file);
            return file.getAbsolutePath(); // 返回文件的绝对路径
        } catch (WriterException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 遍历渠道列表并为每个渠道生成二维码
     *
     * @param channels 渠道列表
     */
    public void generateQRCodesForChannels(List<OmChannel> channels) {
        for (OmChannel channel : channels) {
            String base64QRCode = generateQRCodeAsBase64(channel.getChannelUuid());
            // 可以在这里处理生成的base64二维码，比如保存或进一步处理
            System.out.println("Generated QR Code for Channel: " + channel.getChannelUuid() + ", Base64: " + base64QRCode);
        }
    }


}